package com.jarvis.app.car.service;

import android.os.Build;

import androidx.annotation.RequiresApi;

import com.jarvis.app.car.common.MessageResult;

import com.jarvis.app.car.model.CarTrack;
import com.jarvis.app.car.util.DBUtils;

import java.sql.Connection;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;


public class CarTrackService {

    public MessageResult<CarTrack> getLastOne(long carId) {
        CarTrack carTrack = null;
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from car_track where car_id = ? order by create_time desc limit 1";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setLong(1, carId);
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String x_point = resultSet.getString("x_point");
                String y_point = resultSet.getString("y_point");
                Date createTime = resultSet.getDate("create_time");
                LocalDateTime dateTime = null;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    dateTime = createTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                }

                carTrack = new CarTrack(id, carId, x_point, y_point, dateTime);
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, carTrack);
    }


    public MessageResult<List<CarTrack>> list(long carId, String startDate, String endDate) {
        List<CarTrack> carTracks = new ArrayList<>();
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet resultSet = null;

        try {
            String sql = "select * from car_track where car_id = ? and create_time >=? and create_time <= ?";

            connection = DBUtils.getConnection();
            pst = connection.prepareStatement(sql);
            pst.setLong(1, carId);
            pst.setString(2, startDate);
            pst.setString(3, endDate);
            resultSet = pst.executeQuery();

            while (resultSet.next()) {
                long id = resultSet.getLong("id");
                String x_point = resultSet.getString("x_point");
                String y_point = resultSet.getString("y_point");
                String date = resultSet.getString("create_time");
                LocalDateTime dateTime = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
                    dateTime = LocalDateTime.parse(date, dateTimeFormatter);
                }
                CarTrack carTrack = new CarTrack(id, carId, x_point, y_point, dateTime);
                carTracks.add(carTrack);
            }
        } catch (Exception e) {

            throw new RuntimeException(e);
        } finally {
            try {
                DBUtils.close(resultSet, pst, connection);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }


        return MessageResult.ok(null, carTracks);
    }
}
